const Koa = require('koa'); //导入koa（import vue里面） 导出moduel.exports
const Router = require('@koa/router');
const koajwt = require('koa-jwt') //中间件，洋葱模型
const orderRouter = require('./routes/order');
const CatRouter = require('./routes/cat');
const DogRouter = require('./routes/dog');
const GoodsRouter = require('./routes/goods');
const NoticeRouter = require('./routes/notice')
const userRouter = require('./routes/user');
const { sequelize } = require("./databases/index"); //引入index.js

(async() => { //自执行函数
    await sequelize.sync({ force: false })
})();

const router = new Router();
const koaBody = require('koa-body');
const cors = require('@koa/cors');

const app = new Koa(); //创建koa核心

router.get('/', (ctx, next) => {
    ctx.body = '你好啊!1111';
});

// app.use(async ctx => { //中间件，把响应传出去  asyn标识是异步函数
//     ctx.body = '你好啊!黎雪娅1111'; //ctx：响应体  响应内容：Hello World
// });
// app.use(
//     koajwt({ serect: "fq" }).unless({
//         //unless：排除登录请求，除了登录之外的请求都要验证

//         path: [
//             /^\/user\/login/, /^\/user\/register/, /^\/user\/getAllUser/, /^\/user\/adminLogin/,
//             /^\/user\/getUser/, /^\/user\/deleteUserById/,/^\/user\/search/, /^\/user\/deleteUser/,
//             /^\/user\/addUser/, /^\/user\/getOneUser/, /^\/user\/updateUser/,
//             /^\/order\/deleteOrderById/, /^\/order\/removeAllOrder/, /^\/order\/getAllOrder/,
//             /^\/order\/updateOrder/, /^\/order\/getOneOrder/, /^\/order\/getAllOrders/,
//             /^\/order\/searchOrder/, /^\/order\/findOrders/, /^\/order\/addOrders/,
//         ]
//     })
// );
router.use('/order', orderRouter.routes())
router.use('/cat', CatRouter.routes())
router.use('/dog', DogRouter.routes())
router.use('/goods', GoodsRouter.routes())
router.use('/user', userRouter.routes())
router.use('/notice', NoticeRouter.routes())
app.use(
    cors({
        origin(ctx) {
            if (ctx.url === '/forbin') { //限制跨域范围
                return false
            }
            return "*" //代表服务器谁都可以访问
        }
    })
);
// app.use(cors())
app.use(koaBody());
app.use(router.routes())
app.use(router.allowedMethods());
app.listen(8888); //服务器将会监听运行在8888端口